<template>
  <div class="app-container">
    <el-form :model="searchForm" label-width="80px" @keyup.enter.native="search()">
      <el-row>
        <el-col class="other">
          <el-form-item label="地区搜索" size="small">
            <el-select v-model="searchForm.provinceId" placeholder="请选择省份" @change="provinceChange">
              <el-option value="" label="全部"></el-option>
              <el-option
                v-for="(item,key) in pca[86]"
                :key="key"
                :label="item"
                :value="key">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col class="other">
          <el-form-item label="企业名称" size="small">
            <el-input v-model="searchForm.companyName"></el-input>
          </el-form-item>
        </el-col>
        <el-col class="other">
          <el-form-item label="备案专业" size="small">
            <el-input v-model="searchForm.professions" placeholder="请用逗号分开"></el-input>
          </el-form-item>
        </el-col>
        <el-col class="searchBtn">
          <el-form-item size="small">
            <el-button type="primary" icon="el-icon-search" @click="search()">搜索</el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div class="table-btn">
      <el-button type="primary" size="small" icon="el-icon-plus"
                 v-if="roleType === 0 || roleType === 5 || roleType === 9" @click="addTeam()">添加
      </el-button>
      <el-dialog :title="dialogTitle" :visible.sync="outerVisible" width="440px">
        <el-form ref="teamRow" :rules="rules" :model="teamRow" label-width="100px">
          <el-form-item prop="companyName" label="企业名称" size="small">
            <el-input type="text" v-model="teamRow.companyName"></el-input>
          </el-form-item>
          <el-form-item prop="provinceId" label="地区" size="small">
            <el-select v-model="teamRow.provinceId" placeholder="请选择省份" @change="addressChange">
              <el-option
                v-for="(item,key) in pca[86]"
                :key="key"
                :label="item"
                :value="key">
              </el-option>
            </el-select>
            <el-input type="textarea" v-model="teamRow.address"></el-input>
          </el-form-item>
          <el-form-item label="价格体系" size="small">
            <el-input type="text" v-model="teamRow.priceSystem"></el-input>
          </el-form-item>
          <el-form-item prop="deadline" label="截止有效期" size="small">
            <el-date-picker
              v-model="teamRow.deadline"
              type="date"
              value-format="yyyy-MM-dd"
              placeholder="选择日期">
            </el-date-picker>
          </el-form-item>
          <el-form-item prop="professions" label="备案专业" size="small">
            <el-checkbox-group v-model="teamRow.professions" @change="handleprofessionChange">
              <el-checkbox v-for="item in professionList" :label="item.professionName" :key="item.professionName">
                {{item.professionName}}
              </el-checkbox>
            </el-checkbox-group>
          </el-form-item>
          <el-form-item label="文件上传" size="small">
            <input type="file" ref='infoFile' @change="getInfoFile($event)">
          </el-form-item>
          <el-form-item label="备注说明" size="small">
            <el-input type="textarea" v-model="teamRow.remark"></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="warning" size="small"  @click="submitForm('teamRow')" v-no-more-click>确定</el-button>
          <el-button size="small" @click="outerVisible = false;isAddress=false">取消</el-button>
        </div>
      </el-dialog>
      <!--<el-button size="small" icon="el-icon-upload2">导出</el-button>-->
    </div>
    <my-table
      :data="tableData"
      :col-configs="colConfigs"
      v-loading="listLoading"
      :page-num="pageNum"
      :page-size="pageSize"
    >
      <el-table-column
        slot="opt"
        label="操作"
        fixed="right"
        width="80">
        <template slot-scope="scope">
          <a v-if="scope.row.filePath" target="_blank" :href="GLOBAL.filePathUrl1+encodeURI(scope.row.filePath)">下载</a>
          <el-button
            @click.native.prevent="editTeam(scope.row.recordId)"
            type="text"
            size="small"
            v-if="roleType === 0 || roleType === 5 || roleType === 9"
          >
            修改
          </el-button>
          <el-popover
            placement="top"
            width="160"
            :ref="`popover-${scope.$index}`"
            v-if="roleType === 0 || roleType === 5 || roleType === 9"
          >
            <p>确定删除这条数据吗?</p>
            <div style="text-align: right; margin: 0">
              <el-button size="mini" type="text" @click="$refs[`popover-${scope.$index}`].doClose()">取消</el-button>
              <el-button type="primary" size="mini" @click.native.prevent="deleteRow(scope)">确定</el-button>
            </div>
            <el-button type="text" size="small" slot="reference" v-popover:popover-${scope.$index}>删除</el-button>
          </el-popover>
        </template>
      </el-table-column>
    </my-table>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="pageNum"
      :page-sizes="[10, 20, 30, 40]"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>
  </div>
</template>

<script>
  import {
    deleteRecords,
    getRecordsList,
    insertRecords,
    selectProfessions,
    selectRecords,
    updateRecords
  } from '@/api/records1'
  import myTable from '@/components/myTable'

  import { AreaSelect } from 'vue-area-linkage'
  import { pca } from 'area-data'
  import { formatDate } from '@/utils/index'

  const dateExpired = {
    props: ['colConfig'],
    filters: {
      formatDate(time) {
        var date = new Date(time)
        return formatDate(date, 'yyyy-MM-dd')
      }
    },
    template: `
  <el-table-column :label="colConfig.label" :width="colConfig.width">
      <span slot-scope="scope">
        {{ scope.row[colConfig.prop] | formatDate }}
      </span>
  </el-table-column>
  `
  }

  export default {
    components: {
      myTable,
      AreaSelect
    },
    data() {
      return {
        pca: pca,
        colConfigs: [
          { prop: 'companyName', label: '企业名称', minWidth: '25%', fixed: true },
          { prop: 'address', label: '地区', minWidth: '15%' },
          { prop: 'professions', label: '备案专业', minWidth: '25%' },
          { prop: 'dateExpired', label: '截止有效期', width: '100', component: dateExpired },
          { prop: 'remark', label: '备注说明', minWidth: '20%' },
          { slot: 'opt' }
        ],
        searchForm: {
          recordType: 3,
          provinceId: '',
          cityId: ''
        },
        searchFormList: {
          recordType: 3
        },
        typeNames: [],
        listLoading: true,
        dialogTitle: '新增企业入库备案',
        dialogType: 0,
        teamRow: {
          professions: []
        },
        teamRowClear: {},
        tableData: [],
        pageNum: 1,
        pageSize: 10,
        total: 1,
        outerVisible: false,
        isAddress: true, // 地区新增 修改 摧毁 生成组件
        rules: {
          companyName: [
            { required: true, message: '请选择企业名称', trigger: 'change' }
          ],
          provinceId: [
            { required: true, message: '请选择地区', trigger: 'change' }
          ],
          deadline: [
            { required: true, message: '请选择截止有效期', trigger: 'change' }
          ],
          professions: [
            { required: true, message: '请选择备案专业', trigger: 'change' }
          ]
        },
        visible: false,
        professionList: []
      }
    },
    created() {
      this.teamRowClear = JSON.parse(JSON.stringify(this.teamRow))
      this.fetchData()
      this.selectProfessions()
    },
    computed: {
      roleType() {
        return this.$store.state.user.roleType
      }
    },
    methods: {
      fetchData() {
        this.searchFormList.pageNum = this.pageNum
        this.searchFormList.pageSize = this.pageSize
        this.listLoading = true
        getRecordsList(this.searchFormList).then(response => {
          this.tableData = response.responseBody.list
          this.total = response.responseBody.total
          this.listLoading = false
        })
      },
      selectProfessions() {
        selectProfessions().then(response => {
          this.professionList = response.responseBody
        })
      },
      provinceChange() {
        this.searchForm.cityId = ''
      },
      search() {
        this.pageNum = 1
        this.searchFormList = JSON.parse(JSON.stringify(this.searchForm))
        this.fetchData()
      },
      addTeam() {
        if (this.$refs['teamRow']) {
          this.$refs['teamRow'].resetFields()
        }
        this.outerVisible = true
        this.isAddress = true
        this.dialogTitle = '新增企业入库备案'
        this.teamRow = JSON.parse(JSON.stringify(this.teamRowClear))
        this.dialogType = 1
        if (this.$refs.infoFile) {
          this.$refs.infoFile.value = ''
        }
      },
      editTeam(recordId) {
        if (this.$refs['teamRow']) {
          this.$refs['teamRow'].resetFields()
        }
        this.outerVisible = true
        this.isAddress = true
        this.dialogTitle = '修改企业入库备案'
        this.dialogType = 0
        this.teamRow = JSON.parse(JSON.stringify(this.teamRowClear))
        const params = {
          recordId: recordId
        }
        selectRecords(params).then(response => {
          this.teamRow = response.responseBody
          this.$set(this.teamRow, 'deadline', formatDate(new Date(this.teamRow.dateExpired), 'yyyy-MM-dd'))
          this.teamRow.provinceId = this.teamRow.provinceId.toString()
          this.teamRow.professions = this.teamRow.professions.split(',')
        })
        if (this.$refs.infoFile) {
          this.$refs.infoFile.value = ''
        }
      },
      submitForm(formName) {
        const params = new FormData()
        if (!this.dialogType) {
          params.append('recordId', this.teamRow.recordId)
        }
        params.append('companyName', this.teamRow.companyName)
        params.append('provinceId', this.teamRow.provinceId)
        params.append('address', this.teamRow.address)
        params.append('priceSystem', this.teamRow.priceSystem ? this.teamRow.priceSystem : '')
        params.append('deadline', this.teamRow.deadline)
        params.append('professions', this.teamRow.professions)
        params.append('remark', this.teamRow.remark ? this.teamRow.remark : '')
        params.append('recordType', 3)
        if (this.teamRow.file) {
          params.append('file', this.teamRow.file)
        }
        this.$refs[formName].validate((valid) => {
          if (valid) {
            if (this.dialogType) {
              insertRecords(params).then(response => {
                console.log(response)
                this.outerVisible = false
                this.isAddress = false
                this.fetchData()
              })
            } else {
              updateRecords(params).then(response => {
                console.log(response)
                this.outerVisible = false
                this.isAddress = false
                this.fetchData()
              })
            }
          } else {
            console.log('error submit!!')
            return false
          }
        })
      },
      deleteRow(scope) {
        this.$refs[`popover-${scope.$index}`].doClose()
        const params = {
          recordId: scope.row.recordId,
          isDel: 1
        }
        deleteRecords(params).then(response => {
          this.fetchData()
        })
      },
      handleSizeChange(val) {
        this.pageSize = val
        this.fetchData()
      },
      handleCurrentChange(val) {
        this.pageNum = val
        this.fetchData()
      },
      addressChange(value) {
        this.teamRow.address = pca[86][value]
      },
      handleprofessionChange() {
        console.log(this.teamRow)
      },
      getInfoFile(event) {
        this.teamRow.file = event.target.files[0]
      }
    },
    mounted() {
      console.log(this.pca)
    }
  }
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
  .app-container {
    .el-pagination {
      margin-top: 20px;
    }
    .table-btn {
      margin-bottom: 10px;
      & > button {
        margin: 0 20px;
      }
      .el-dialog {
        textarea {
          margin-top: 10px;
        }
        .el-checkbox {
          margin-right: 20px;
        }
        .el-checkbox + .el-checkbox {
          margin-left: 0;
        }
      }
    }
  }

</style>


<style rel="stylesheet/scss" lang="scss">
  .el-input__icon {
    line-height: 100%;
  }

  .el-form-item__content {
    line-height: 33px;
  }

  .area-select .area-selected-trigger {
    padding: 0 10px;
  }

  a, a:focus, a:hover {
    color: #409EFF;
  }

  /*.el-table__body tr.hover-row>td {
    background-color: #0000ff !important;
  }

  .el-table__body tr.current-row>td {
    background-color: #0000ff !important;
  }*/
</style>


